package com.hb.software.salary.impl;

import com.hb.software.entity.salary.Salary;
import com.hb.software.salary.SalaryService;
import com.hb.software.salary.dao.SalaryDao;
import com.hb.software.service.impl.BaseServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional
public class SalaryServiceImpl extends BaseServiceImpl<Salary, Long> implements SalaryService {

    private static final Logger logger = LogManager.getLogger(SalaryServiceImpl.class.getName());

    private SalaryDao salaryDao;

    @Autowired
    public void setSalaryDao(SalaryDao salaryDao) {
        this.salaryDao = salaryDao;
        this.dao = this.salaryDao;
    }

    @Override
    public List findByName(String name) {
        return salaryDao.findByName(name);
    }

    @Override
    public List<Salary> findSalaryAll() {
        return salaryDao.findAll();
    }

    @Override
    public Integer batchDelete(List<Long> ids) {
        return salaryDao.batchDelete(ids);
    }

    @Override
    public String createExcel(List<Salary> list, String path) {

        // 创建一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = workbook.createSheet("薪资表一");
        // 添加表头行
        HSSFRow hssfRow = sheet.createRow(0);
        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
//    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 添加表头内容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("编号");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("姓名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("基础工资");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("实际工资");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(4);
        headCell.setCellValue("发薪日");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(5);
        headCell.setCellValue("薪资加成");
        headCell.setCellStyle(cellStyle);

        // 添加数据内容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 1);
            Salary salary = list.get(i);

            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(salary.getId());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(1);
            cell.setCellValue(salary.getName());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(2);
            cell.setCellValue(salary.getBasicSalary());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(3);
            cell.setCellValue(salary.getSalary());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(4);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = sdf.format(salary.getDatetime());
            cell.setCellValue(time);
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(5);
            cell.setCellValue(salary.getProfitBonus());
            cell.setCellStyle(cellStyle);

        }

        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream(path);
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }

    @Override
    public List<Salary> findDatetime(Date startTime, Date endTime) {
        return salaryDao.findDatetime(startTime,endTime);
    }


    @Override
    public List<Salary> findDatetimeById(Date startTime, Date endTime,Long wid) {
        return salaryDao.findDatetimeById(startTime,endTime,wid);
    }

    @Override
    public List<Salary> findById(Long wid){
        return salaryDao.findById(wid);
    }
//
//
//    @Override
//    public List<Salary> findAll() {
//        List<Salary> list = this.dao.findAll();
//        //删除列表重复项
//        list= list.stream().distinct().collect(Collectors.toList());
//        return list;
//    }
}
